package com.metadata_system.api_server;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.client.naming.utils.SignUtil;
import com.metadata_system.common.utils.ApiSignUtils;
import com.metadata_system.qt_client.service.ProjectService;
import com.metadata_system.common.db.entity.*;
import com.metadata_system.common.db.mapper.*;
import com.metadata_system.common.dto.ProjectLogCount;
import com.metadata_system.system.request.ProjectLogListParams;
import com.metadata_system.system.response.NodePropertiesItem;
import com.metadata_system.system.service.ProjectLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.spring.annotation.MapperScan;
import com.metadata_system.common.dto.ApiResult;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@SpringBootApplication(scanBasePackages = {"com.metadata_system.*"})
@MapperScan(basePackages = {"com.metadata_system.common.db.mapper"})
@RestController
@Slf4j
@EnableScheduling
public class ApiServerApplication {


    @Value("${spring.application.name}")
    String springApplicationName;

    @Value("${server.port:7300}")
    String serverPort;

    @Value("${socketio.port}")
    String socketioPort;

    @Autowired(required = false)
    MetadataMapper metadataMapper;

    @Autowired
    ProjectLogService projectLogService;
    @Autowired
    private ProjectService projectService;

    public static void main(String[] args) {
        SpringApplication.run(ApiServerApplication.class, args);
    }

    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationReady() {
        log.info("==========================================");
        log.info("🚀 服务器启动完毕!");
        log.info("📱 应用名称: {}", springApplicationName);
        log.info("🌐 API地址: http://localhost:{}/", serverPort);
        log.info("🌐 SocketIO服务端地址: http://localhost:{}/", socketioPort);
        log.info("🌐 测试API地址: http://localhost:{}/test", serverPort);
        log.info("🔍 健康检查: http://localhost:{}/healthz", serverPort);
        log.info("==========================================");
    }

    @RequestMapping(value = "/", method = {RequestMethod.POST, RequestMethod.GET})
    public ApiResult index() {
        List<Metadata> list = this.metadataMapper.selectAll();
        for (Metadata metadata : list) {
            this.metadataMapper.updateMetadataStats(metadata.getId());
        }



        return ApiResult.success(springApplicationName);
    }

    // 健康检查
    @RequestMapping("/healthz")
    public Object healthz(
            @RequestParam(
                    value = "name",
                    required = false,
                    defaultValue = ""
            ) String name
    ) {
        return ApiResult.success("name: " + name);
    }

    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired(required = false)
    ProjectMapper projectMapper;
    @Autowired(required = false)
    ProjectNodeMapper projectNodeMapper;
    @Autowired(required = false)
    MetadataNodeMapper metadataNodeMapper;
    @Autowired(required = false)
    DatabaseMapper databaseMapper;
    @Autowired(required = false)
    ProjectLogMapper projectLogMapper;

    @RequestMapping("/test")
    @Transactional
    public Object test() {
        return ApiResult.success();
    }

}
